<?php
// +----------------------------------------------------------------------
// | Widget管理
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: WidgetAction.class.php 88 2011-04-04 12:29:32Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class WidgetAction extends BaseAction {

	public function index() {
		redirect(__APP__);
	}
	
	public function listWidget() {
	
	    /* Widget原始数据 */
		$widget_list = array('RandomPosts','Calendar','RecentComments','Categories','TagCloud','Cumulus','Polls','Archives','Links','Statistics');
		$init_widget_title = unserialize('a:10:{s:11:"RandomPosts";a:2:{s:5:"title";s:12:"Random Posts";s:11:"description";s:12:"随机日志";}s:8:"Calendar";a:2:{s:5:"title";s:8:"Calendar";s:11:"description";s:27:"您站点的日志日程表";}s:14:"RecentComments";a:2:{s:5:"title";s:15:"Recent Comments";s:11:"description";s:39:"在侧边栏显示最新评论的列表";}s:10:"Categories";a:2:{s:5:"title";s:10:"Categories";s:11:"description";s:30:"您站点日志的分类目录";}s:8:"TagCloud";a:2:{s:5:"title";s:4:"Tags";s:11:"description";s:27:"您最常使用的标签云";}s:7:"Cumulus";a:2:{s:5:"title";s:7:"Cumulus";s:11:"description";s:26:"一个球状的3D标签云";}s:5:"Polls";a:2:{s:5:"title";s:5:"Polls";s:11:"description";s:30:"随机一个已激活的投票";}s:8:"Archives";a:2:{s:5:"title";s:8:"Archives";s:11:"description";s:30:"您站点日志的月度存档";}s:5:"Links";a:2:{s:5:"title";s:5:"Links";s:11:"description";s:18:"友情链接列表";}s:10:"Statistics";a:2:{s:5:"title";s:10:"Statistics";s:11:"description";s:30:"您站点的博客信息统计";}}');
	    $widget_off = (C('off_widget')) ? unserialize(C('off_widget')) : $widget_list;
		$widget_on  =  unserialize(C('on_widget'));
		$widget_custom  =  unserialize(C('custom_widget'));
		$widget_title = (C('tilte_widget')) ? unserialize(C('tilte_widget')) : $init_widget_title;
		foreach ($widget_title as $k=>$v) {
			if (in_array($k,$widget_off)) {
				$new_widget_off[$k] = $widget_title[$k];
			}
		}
		foreach ($widget_title as $k=>$v) {
			if (in_array($k,$widget_on)) {
				$new_widget_on[$k] = $widget_title[$k];
			}
		}
		/*echo "<pre>";
		print_r($new_widget_on);
		echo "</pre>";*/
		$this->assign('action_link', array('href'=>__URL__.'/editWidget/', 'text' => '插件列表'));
		$this->assign('ur_here','widget管理');
		$this->assign('widget_on',$widget_on);
		$this->assign('widget_off',$new_widget_off);
		$this->assign('widget_custom',$widget_custom);
		$this->display();
	}
	
	public function saveWidget() {
		if($_POST['ajax']) {
			$widget_list = array('RandomPosts','Calendar','RecentComments','Categories','TagCloud','Cumulus','Polls','Archives','Links','Statistics');
			$widget_on   = $_POST['offWidget'];
			if(is_array($widget_on)) {
				$widget_off  =  serialize(array_diff($widget_list,$widget_on));
				$widget_on   =  serialize($widget_on);
			} else {
				$widget_off  = serialize($widget_list);
			}
			$conf  =  M('Config');
			$conf->where("config_name = 'on_widget'")->setField('config_value', $widget_on);
			$conf->where("config_name = 'off_widget'")->setField('config_value', $widget_off);	
			set_log('edit(Widget)');
			clear_all_files();
			$this->success();
		} else {
			$this->error('非法操作');
		}
	}
	
	public function customWidget() {
	
		if($_POST['act']) {
		
			$custom_widget = (C('custom_widget')) ? unserialize(C('custom_widget')) : array();
			$new_title = isset($_POST['custom_widget_title']) ? $_POST['custom_widget_title'] : '';
			$new_content = isset($_POST['custom_widget_content']) ? $_POST['custom_widget_content'] : '';

			if($new_content) {
				$i = 0;
				$maxKey = 0;
				if(is_array($custom_widget)) {
					foreach ($custom_widget as $key => $val) {
						preg_match("/^Custom(\d+)/", $key, $matches);
						$k = $matches[1];
						if($k > $i) {
							$maxKey = $k;
						}
						$i = $k;
					}
				}
				$custom_wg_index = $maxKey + 1;
				$custom_wg_index = 'Custom'.$custom_wg_index;
				$custom_widget[$custom_wg_index] = array('title'=>$new_title,'description'=>$new_content);
				$custom_widget_str = serialize($custom_widget);
				$conf  =  M('Config');
				$conf->where("config_name = 'custom_widget'")->setField('config_value', $custom_widget_str);
				set_log('add(Custom Widget)');
				clear_all_files();
				$this->assign('jumpUrl',__URL__.'/listWidget/');
				$this->success('添加成功');
			}	
		} else {
			$this->error('非法操作');
		}
	}
	
	public function deleteCustomWidget() {
	
		$custom_widget = C('custom_widget') ? unserialize(C('custom_widget')) : array();
		$custom_id = isset($_GET['id']) ? addslashes($_GET['id']) : '';	
		$widget_on  =  unserialize(C('on_widget'));
		
		if(is_array($widget_on) && !empty($widget_on)) {
			foreach ($widget_on as $key => $val) {
				if($val == $custom_id) {
					unset($widget_on[$key]);
				}
			}
			$widgets_str = serialize($widget_on);
			$conf  =  M('Config');
			$conf->where("config_name = 'on_widget'")->setField('config_value', $widgets_str);
			unset($custom_widget[$custom_id]);
			$custom_widget_str = serialize($custom_widget);
			$conf->where("config_name = 'custom_widget'")->setField('config_value', $custom_widget_str);
			set_log('drop(Custom Widget, Id: '.$custom_id.')');
			clear_all_files();
			redirect(__URL__.'/listWidget/');
		}
	}

}

?>